﻿Imports Odyssee.WebControles
Imports OpenDelib.ServiceDeliberations

Public Class FicheCommission
    Inherits FicheDossierPage

    Protected ReadOnly Property TypeRelationParticipant As String
        Get
            Return Tools.GetInstance().Parametre(TypeParametre.TypeRelationParticipant)
        End Get
    End Property

    Public Overrides ReadOnly Property Titre As String
        Get
            Return "Fiche commission"
        End Get
    End Property
    Protected Overrides ReadOnly Property TypeDelib As TypeDeliberation
        Get
            Return TypeDeliberation.Commission
        End Get
    End Property
    Protected Overrides ReadOnly Property LibelleEntite As String
        Get
            Return "commission"
        End Get
    End Property
    Protected Overrides ReadOnly Property LibelleFeminin As Boolean
        Get
            Return True
        End Get
    End Property
    Protected Overrides ReadOnly Property LibelleApostrophe As Boolean
        Get
            Return False
        End Get
    End Property
    Protected Overrides ReadOnly Property UtiliseDocuments As Boolean
        Get
            Return False
        End Get
    End Property
    Protected Overrides ReadOnly Property PopupPanel As JQPopupPanel
        Get
            Return Me.pnlCommission
        End Get
    End Property
    Protected Overrides ReadOnly Property BoutonActualiser As Button
        Get
            Return Me.btnActualiser
        End Get
    End Property
    Protected Overrides ReadOnly Property SeparateurActualiser As ToolBarSeparator
        Get
            Return Me.tbSep1
        End Get
    End Property
    Protected Overrides ReadOnly Property BoutonEnregistrer As Button
        Get
            Return Me.btnEnregister
        End Get
    End Property
    Protected Overrides ReadOnly Property SeparateurTraitements As ToolBarSeparator
        Get
            Return Me.tbSep2
        End Get
    End Property
    Protected Overrides ReadOnly Property BoutonTraitements As ToolBarTextImageMenuButton
        Get
            Return Me.btnTraitements
        End Get
    End Property


    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            If Request.Form("__EVENTTARGET") = "btnAjouterContact" Then
                ' Ajouter lien(s) contact(s)
                Dim clesContact As String = Request.Form("ctl00$content$pnlCommission__hidden1")
                If IsNumeric(clesContact) Then
                    AjouterLienContact(CInt(clesContact))
                ElseIf clesContact.Contains("¤") Then
                    For Each cleContact As String In clesContact.Split("¤"c)
                        If IsNumeric(cleContact) Then AjouterLienContact(CInt(cleContact))
                    Next
                End If
            End If
        Else
            SetFocus(Me.txtObjet)
        End If
    End Sub

    Private Sub gvContacts_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gvContacts.RowCommand
        Select Case e.CommandName.ToUpper
            Case "AJOUTER"

            Case "SUPPRIMER"
                Dim cleContact As Integer = 0
                Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
                cleContact = CInt(row.Cells(0).Text)
                SupprimerLienContact(cleContact)
        End Select
    End Sub


    Protected Overrides Sub InitialiserParametres()
        MyBase.InitialiserParametres()
    End Sub

    Protected Overrides Sub InitialiserPage()
        MyBase.InitialiserPage()

        Select Case Me.ModeFiche

            Case Tools.ModeFiche.Creation
                Me.divContacts.Visible = False

            Case Tools.ModeFiche.Modification
                ' Dossier
                Me.txtObjet.Text = Me.Entite.Identificateur
                Me.txtResume.Text = Me.Entite.Remarques
                Me.txtCode.Text = Me.Entite.Code

                ' Membres (contacts liés)
                InitialiserLiensContacts()

        End Select
    End Sub

    Protected Overrides Function Renseigner() As Boolean
        If Not MyBase.Renseigner() Then Return False
        Entite.Identificateur = Me.txtObjet.Text
        Entite.Remarques = Me.txtResume.Text
        Return True
    End Function

    Protected Overrides Function Creer() As Boolean
        If Not MyBase.Creer() Then Return False
        Return True
    End Function

    Protected Overrides Function MettreAJour() As Boolean
        If Not MyBase.MettreAJour() Then Return False
        Return True
    End Function


    Private Sub InitialiserLiensContacts()
        Using svc As New ServiceContacts.ContactsClient
            Dim params As New ServiceContacts.CriteresRechercheContact
            params.Authentication = New ServiceContacts.AuthenticationParameters
            params.Authentication.Token = Tools.GetInstance.getToken
            params.DossierLie = Me.CleEntite
            params.TypeRelationDossierLie = Me.TypeRelationParticipant

            Dim results As ServiceContacts.ResultatRechercheContact = Nothing
            Try
                results = svc.Recherche(params)
                If results Is Nothing OrElse Not results.OK Then
                    NotifyErreur("Impossible de charger les membres de la commission '" & Me.CleEntite & "'" & If(results Is Nothing, ".", " :" & ControlChars.NewLine & results.ErrorMessage))
                Else
                    Me.gvContacts.DataSource = results.Contacts
                    Me.gvContacts.DataBind()
                End If
            Catch ex As Exception
                NotifyErreur(ex.Message)
            End Try
        End Using
    End Sub
    Protected Sub AjouterLienContact(cleContact As Integer)
        Using svc As New ServiceDossiers.DossiersClient
            Dim params As New ServiceDossiers.ParametresLienDossierEntrepriseContact
            params.Authentication = New ServiceDossiers.AuthenticationParameters
            params.Authentication.Token = Tools.GetInstance.getToken
            params.CodeDossier = Me.CleEntite
            params.CleContact = cleContact
            params.TypeRelation = Me.TypeRelationParticipant

            Try
                Dim results As ServiceDossiers.OperationResult
                results = svc.AjouterLienActeur(params)
                If results.OK Then
                    NotifySuccess("Ajout effectué avec succès.")
                    InitialiserLiensContacts()
                Else
                    NotifyErreur(results.ErrorMessage)
                End If
            Catch ex As Exception
                NotifyErreur(ex.Message)
            End Try
        End Using
    End Sub
    Protected Sub SupprimerLienContact(cleContact As Integer)
        Using svc As New ServiceDossiers.DossiersClient
            Dim params As New ServiceDossiers.ParametresLienDossierEntrepriseContact
            params.Authentication = New ServiceDossiers.AuthenticationParameters
            params.Authentication.Token = Tools.GetInstance.getToken
            params.CodeDossier = Me.CleEntite
            params.CleContact = cleContact
            params.TypeRelation = Me.TypeRelationParticipant

            Try
                Dim results As ServiceDossiers.OperationResult
                results = svc.SupprimerLienActeur(params)
                If results.OK Then
                    NotifySuccess("Suppression effectuée avec succès.")
                    InitialiserLiensContacts()
                Else
                    NotifyErreur(results.ErrorMessage)
                End If
            Catch ex As Exception
                NotifyErreur(ex.Message)
            End Try
        End Using
    End Sub

End Class